package com.whenjun.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ifootbook.util.EmptyUtil;
import com.ifootbook.util.FootBookException;
import com.whenjun.dto.base.BaseId;
import com.whenjun.dto.kq.KqLocationGroupAddReq;
import com.whenjun.dto.kq.KqLocationGroupDTO;
import com.whenjun.dto.kq.KqLocationGroupReq;
import com.whenjun.dto.kq.KqLocationUpdReq;
import com.whenjun.dto.person.IscPersonDTO;
import com.whenjun.entity.IscPerson;
import com.whenjun.entity.KqGroup;
import com.whenjun.entity.KqLocation;
import com.whenjun.entity.KqLocationGroup;
import com.whenjun.enums.KqDoorTypeEnum;
import com.whenjun.mapper.KqLocationGroupMapper;
import com.whenjun.service.IKqLocationGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.IPageUtil;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-21
 */
@Service
public class KqLocationGroupServiceImpl extends MPJBaseServiceImpl<KqLocationGroupMapper, KqLocationGroup> implements IKqLocationGroupService {

//    @Autowired
//    private KqLocationServiceImpl kqLocationService;

    @Autowired
    private KqLocationGroupDoorServiceImpl kqLocationGroupDoorService;

    @Transactional
    public IPage<KqLocationGroupDTO> KqLocationGroups(KqLocationGroupReq kqLocationGroupReq) {
        Page page = new Page<>(kqLocationGroupReq.getPage(), kqLocationGroupReq.getSize());

        // 改版前
//        IPage<KqLocationGroup> kqLocationGroupIPage = this.lambdaQuery()
//                // .like(!EmptyUtil.isEmpty(kqLocationGroupReq.getName()), KqLocationGroup::getName, kqLocationGroupReq.getName())
//                .eq(KqLocationGroup::getLocationId, kqLocationGroupReq.getLocationId())
//                .orderByDesc(KqLocationGroup::getCreateTime)
//                .page(page)
//                ;
//        IPage<KqLocationGroupDTO> kqLocationGroupDTOIPage = IPageUtil.trans(kqLocationGroupIPage);


        MPJLambdaWrapper<KqLocationGroup> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(KqLocationGroup.class)
                .select(KqGroup::getName)
                .leftJoin(KqGroup.class, KqGroup::getType, KqLocationGroup::getGroupCode)
                .like(!EmptyUtil.isEmpty(kqLocationGroupReq.getName()), KqGroup::getName, kqLocationGroupReq.getName())
                .eq(KqLocationGroup::getLocationId, kqLocationGroupReq.getLocationId())
                .orderByDesc(KqLocationGroup::getCreateTime);

        IPage<KqLocationGroupDTO> kqLocationGroupDTOIPage = this.getBaseMapper().selectJoinPage(page, KqLocationGroupDTO.class, wrapper);

        return kqLocationGroupDTOIPage;
    }

    @Transactional
    public void KqLocationGroupAdd(KqLocationGroupAddReq kqLocationGroupAddReq) {
//        KqLocation kqLocation = kqLocationService.getById(kqLocationGroupAddReq.getLocationId());
//        if (kqLocation == null || kqLocation.getState().equals(true)){
//            throw new FootBookException("locationId错误");
//        }

        KqDoorTypeEnum.checkIn(kqLocationGroupAddReq.getGroupCode());

        KqLocationGroup kqLocationGroup = this.lambdaQuery()
                .eq(KqLocationGroup::getLocationId, kqLocationGroupAddReq.getLocationId())
                .eq(KqLocationGroup::getGroupCode, kqLocationGroupAddReq.getGroupCode())
                .eq(KqLocationGroup::getState, false)
                .one();

        if (kqLocationGroup != null) {
            throw new FootBookException("groupCode已存在，请修改后再试");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocationGroup = new KqLocationGroup();
        kqLocationGroup.setLocationId(kqLocationGroupAddReq.getLocationId());
        kqLocationGroup.setState(false);
        kqLocationGroup.setGroupCode(kqLocationGroupAddReq.getGroupCode());
        kqLocationGroup.setCreateUser(userId);
        kqLocationGroup.setCreateTime(now);

        this.save(kqLocationGroup);
    }

    @Transactional
    public void KqLocationGroupDel(BaseId baseId) {
        KqLocationGroup kqLocationGroup = this.getById(baseId.getId());
        if (kqLocationGroup == null) {
            throw new FootBookException("id错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocationGroup.setState(true);
        kqLocationGroup.setUpdateUser(userId);
        kqLocationGroup.setUpdateTime(now);

//        this.updateById(kqLocationGroup);
        this.removeById(kqLocationGroup);
        kqLocationGroupDoorService.KqLocationGroupDoorRmByLocationGroupId(kqLocationGroup.getLocationId());
    }

    @Transactional
    public void KqLocationGroupDelByLocationId(String locationId) {
        List<KqLocationGroup> kqLocationGroups = this.lambdaQuery()
                .eq(KqLocationGroup::getLocationId, locationId)
                .list();

        for (KqLocationGroup kqLocationGroup : kqLocationGroups) {
            this.removeById(kqLocationGroup);

            // 物理删除 KqLocationGroupDoor List
            kqLocationGroupDoorService.KqLocationGroupDoorRmByLocationGroupId(kqLocationGroup.getLocationId());
        }
    }

    @Transactional
    public void KqLocationGroupUpd(KqLocationUpdReq kqLocationUpdReq) {
        KqLocationGroup kqLocationGroup = this.getById(kqLocationUpdReq.getId());
        if (kqLocationGroup == null) {
            throw new FootBookException("id错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocationGroup.setUpdateUser(userId);
        kqLocationGroup.setUpdateTime(now);
        // kqLocationGroup.setName(kqLocationUpdReq.getName());

        this.updateById(kqLocationGroup);
    }
}
